package com.redstars.generate.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.NamingCase;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.redstars.generate.exception.RedstarsException;
import com.redstars.generate.page.PageResult;
import com.redstars.generate.utils.PageUtils;
import lombok.AllArgsConstructor;
import com.redstars.generate.convert.SysMainSubTableConvert;
import com.redstars.generate.entity.SysMainSubTable;
import com.redstars.generate.query.SysMainSubTableQuery;
import com.redstars.generate.vo.SysMainSubTableVo;
import com.redstars.generate.dao.SysMainSubTableDao;
import com.redstars.generate.service.SysMainSubTableService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;

/**
 * 主子表关联表
 *
 * @author zhouhx 
 * @since 1.0.0 2022-11-08
 */
@Service
@AllArgsConstructor
public class SysMainSubTableServiceImpl extends ServiceImpl<SysMainSubTableDao, SysMainSubTable> implements SysMainSubTableService {
    @Override
    public void save(SysMainSubTableVo vo) {
        SysMainSubTable entity = SysMainSubTableConvert.INSTANCE.convert(vo);
        baseMapper.insert(entity);
    }

    @Override
    public void update(SysMainSubTableVo vo) {
        SysMainSubTable entity = SysMainSubTableConvert.INSTANCE.convert(vo);
        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

    @Override
    public PageResult<SysMainSubTableVo> page(SysMainSubTableQuery query) {
        LambdaQueryWrapper<SysMainSubTable> wrapper = getWrapper(query);
        PageUtils<SysMainSubTable> pageUtils = new PageUtils<>();
        IPage<SysMainSubTable> page = this.page(pageUtils.getPage(query), wrapper);

        return pageUtils.getPageData(page,SysMainSubTableVo.class);
    }

    private LambdaQueryWrapper<SysMainSubTable> getWrapper(SysMainSubTableQuery query){
        LambdaQueryWrapper<SysMainSubTable> wrapper = Wrappers.lambdaQuery();
        if(ObjectUtil.isNotEmpty(query.getId())){
            wrapper.eq(SysMainSubTable::getId,query.getId());
        }
        if(ObjectUtil.isNotEmpty(query.getMainTableId())){
            wrapper.eq(SysMainSubTable::getMainTableId,query.getMainTableId());
        }
        if(ObjectUtil.isNotEmpty(query.getSubTableId())){
            wrapper.eq(SysMainSubTable::getSubTableId,query.getSubTableId());
        }
        if(ObjectUtil.isNotEmpty(query.getMianTableFkName())){
            wrapper.eq(SysMainSubTable::getMianTableFkName,query.getMianTableFkName());
        }
        if(ObjectUtil.isNotEmpty(query.getSubTableFkName())){
            wrapper.eq(SysMainSubTable::getSubTableFkName,query.getSubTableFkName());
        }
        return wrapper;
    }
    private QueryWrapper<SysMainSubTable> getQueryWrapper(SysMainSubTableQuery query){
        QueryWrapper<SysMainSubTable> wrapper = new QueryWrapper<>();

        if(ObjectUtil.isNotEmpty(query.getSubTableId())){
            wrapper.eq("sys_main_sub_table.sub_table_id",query.getSubTableId());
        }
        if(ObjectUtil.isNotEmpty(query.getMainTableId())){
            wrapper.eq("sys_main_sub_table.main_table_id",query.getMainTableId());
        }
        return wrapper;
    }
    @Override
    public SysMainSubTableVo detail(Long id) {
        SysMainSubTable detail = this.getById(id);
        if (ObjectUtil.isEmpty(detail)) {
        throw new RedstarsException("记录不存在");
        }
        return BeanUtil.copyProperties(detail,SysMainSubTableVo.class);
    }

    @Override
    public List<SysMainSubTableVo>  list(SysMainSubTableQuery query){
       QueryWrapper<SysMainSubTable> wrapper = getQueryWrapper(query);
       List<SysMainSubTableVo> list = baseMapper.list(wrapper);
       if(CollUtil.isNotEmpty(list)){
           list.forEach(mainSub ->{
               mainSub.setSubFiledName(StrUtil.lowerFirst(mainSub.getSubClassName()));
               mainSub.setMainFiledName(StrUtil.lowerFirst(NamingCase.toPascalCase(mainSub.getMianTableFkName())));
           });
       }
       return list;
    }

    public static void main(String[] args) {
        System.out.println(StrUtil.lowerFirst(NamingCase.toPascalCase("sys_main_sub_table")));
    }
}